import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

axios.defaults.baseURL = 'https://www.liulongbin.top:8888/api/private/v1/'
axios.interceptors.request.use(config => {
  NProgress.start()
  config.headers.Authorization = window.sessionStorage.getItem('token')
  return config
})
axios.interceptors.response.use(config => {
  NProgress.done()
  return config
})

export default function request (url, data = {}, method = 'get'){
  return new Promise((resolve,reject) => {
    let p =null
    if (method === 'get'){
      p = axios.get(url, {
        params: data
      })
    }else if (method === 'post') {
      p = axios.post(url, data)
    }else if (method === 'put') {
      p = axios.put(url, data)
    }else if (method === 'delete') {
      p = axios.delete(url, data)
    }
    p.then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}
